package com.leetcode.No0497;

import java.util.Arrays;
import java.util.Random;

/**
 * @program: Solution
 * @description: 非重叠矩形中的随机点
 * 此算法考虑欠缺，因为题目要求的是所有矩形中的所有整数点的概率都一样，概率一样的主体是整数点
 * 但本算法中先是让所有矩形被选的概率一样，然后让选中的矩阵中的整数点被选的概率一样，这是不对的
 * @author: Wang Zhihua
 * @date: 2022-06-09
 */
public class Solution {

    private int[][] rects;
    private int rectsSize;
    private Random random;

    public Solution(int[][] rects) {
        this.rects = rects;
        rectsSize = rects.length;
    }

    public int[] pick() {
        random = new Random();
        int i = random.nextInt(rectsSize);

        int x1 = rects[i][0]; // 矩形左下角x
        int y1 = rects[i][1]; // 矩形左下角y
        int x2 = rects[i][2]; // 矩形右上角x
        int y2 = rects[i][3]; // 矩形右上角y

        int[] result = new int[2];
        result[0] = x1 + random.nextInt(x2 - x1 + 1);
        result[1] = y1 + random.nextInt(y2 - y1 + 1);

        return result;
    }

    public static void main(String[] args) {
        int[][] nums = {{82918473,-57180867,82918476,-57180863},{83793579,18088559,83793580,18088560},
                {66574245,26243152,66574246,26243153},{72983930,11921716,72983934,11921720}};
        Solution solution = new Solution(nums);
        for (int i = 0; i< 10; ++i) {
            int[] pick = solution.pick();
            System.out.println(Arrays.toString(pick));
        }
    }
}
